package com.wkbb.common.dto;

import java.io.Serializable;

import com.wkbb.common.constants.ErrorCode;

import com.wkbb.common.utils.StringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * API响应结果
 *
 * @param <T>
 * @author
 * @update
 * @since 1.0
 */
@ApiModel(value = "ResultDTO", description = "json返回值工具类")
public class ResultDTO<T> implements Serializable {

    private static final long serialVersionUID = 101539585470879542L;

    @ApiModelProperty(value = "返回的数据对象")
    private T data; // 返回的数据对象

    @ApiModelProperty(value = "业务逻辑是否调用成功")
    private boolean success = true; // 业务逻辑是否调用成功

    @ApiModelProperty(value = "错误码")
    private String errorCode; // 错误码

    @ApiModelProperty(value = "错误信息")
    private String errorMsg; // 错误信息

    public static ResultDTO errorResult(String errMsg){
        return new ResultDTO(false,"30001",errMsg);
    }

    public static ResultDTO successResult(){
        return new ResultDTO();
    }

    public static ResultDTO successResult(Object data){
        return new ResultDTO(data);
    }

    /**
     * 默认success=true.
     */
    public ResultDTO() {
        this.success = true;
        this.errorCode = ErrorCode.SUCCESS.getCode();
        this.errorMsg = ErrorCode.SUCCESS.getMessage();
    }

    public ResultDTO(boolean success) {
        if(success){
            this.success = true;
            this.errorCode = ErrorCode.SUCCESS.getCode();
            this.errorMsg = ErrorCode.SUCCESS.getMessage();
        }else{
            this.success = false;
            this.errorCode = ErrorCode.SYSTEM_EXCEPTION.getCode();
            this.errorMsg = ErrorCode.SYSTEM_EXCEPTION.getMessage();
        }
    }

    /**
     * 默认success=false.
     */
    public ResultDTO(ErrorCode errorCode) {
        this.success = false;
        this.errorCode = errorCode.getCode();
        this.errorMsg = errorCode.getMessage();
    }

    public ResultDTO(boolean success,String errorCode, String errorMsg) {
        this.success = success;
        this.errorCode =errorCode;
        this.errorMsg = errorMsg;
    }

    /**
     * 默认success=true.
     */
    public ResultDTO(T data) {
        this.success = true;
        this.data = data;
        this.errorCode = ErrorCode.SUCCESS.getCode();
        this.errorMsg = ErrorCode.SUCCESS.getMessage();
    }

    /**
     * @return the data
     */
    public T getData() {
        return this.data;
    }

    /**
     * @param data the data to set
     */
    public void setData(T data) {
        this.data = data;
    }

    /**
     * @return the success
     */
    public boolean isSuccess() {
        return this.success;
    }

    /**
     * @param success the success to set
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * @return the errorCode
     */
    public String getErrorCode() {
        return this.errorCode;
    }

    /**
     * @param errorCode the errorCode to set
     */
    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    /**
     * @return the errorMsg
     */
    public String getErrorMsg() {
        return this.errorMsg;
    }

    /**
     * @param errorMsg the errorMsg to set
     */
    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    @Override
    public String toString() {
        return "ResultDTO{" + "data=" + data + ", success=" + success + ", errorCode='" + errorCode + '\'' + ", errorMsg='" + errorMsg + '}';
    }

    /**
     * 返回接口返回值成功还是异常
     */
    public static boolean checkSuccess(ResultDTO resultDTO) {
        if(resultDTO.isSuccess() && !StringUtil.isEmpty(resultDTO.getErrorCode()) && resultDTO.getErrorCode().equals(ErrorCode.SUCCESS.getCode())){
            return  true;
        }else {
            return false;
        }
    }
}
